package cc.rengu.redp.hawk.config;

import cc.rengu.redp.common.properties.RedpProperties;
import liquibase.integration.spring.SpringLiquibase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;

import javax.sql.DataSource;

@Configuration
public class LiquibaseHawkConfig {

    @Autowired
    private RedpProperties redpProperties;

    @Bean
    public SpringLiquibase hawkLiquibase(DataSource dataSource) {
        SpringLiquibase liquibase = new SpringLiquibase();
        // 用户模块Liquibase文件路径
        liquibase.setChangeLog("classpath:db/liquibase/hawkChangelog.xml");
        liquibase.setDataSource(dataSource);
        liquibase.setShouldRun(redpProperties.isLiquibaseEnable());
        liquibase.setResourceLoader(new DefaultResourceLoader());
        // 覆盖Liquibase changelog表名
        liquibase.setDatabaseChangeLogTable("REDP_HAWK_DBCHANGELOG");
        liquibase.setDatabaseChangeLogLockTable("REDP_HAWK_DBCHANGELOGLOCK");
        return liquibase;
    }
}